package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.core.domain.model.LoginAppUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.AppArtifact;
import com.ruoyi.system.mapper.AppArtifactMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AppReviewMapper;
import com.ruoyi.system.domain.AppReview;
import com.ruoyi.system.service.IAppReviewService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginAppUser;

/**
 * 评价Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@Service
public class AppReviewServiceImpl implements IAppReviewService 
{
    @Autowired
    private AppReviewMapper appReviewMapper;
    @Autowired
    private AppArtifactMapper appArtifactMapper;

    /**
     * 查询评价
     * 
     * @param reviewId 评价主键
     * @return 评价
     */
    @Override
    public AppReview selectAppReviewByReviewId(Long reviewId)
    {
        return appReviewMapper.selectAppReviewByReviewId(reviewId);
    }

    /**
     * 查询评价列表
     * 
     * @param appReview 评价
     * @return 评价
     */
    @Override
    public List<AppReview> selectAppReviewList(AppReview appReview)
    {
        return appReviewMapper.selectAppReviewList(appReview);
    }

    /**
     * 新增评价
     * 
     * @param appReview 评价
     * @return 结果
     */
    @Override
    public int insertAppReview(AppReview appReview)
    {
        LoginAppUser loginAppUser = getLoginAppUser();
        if (Objects.isNull(loginAppUser)) {
            throw new ServiceException("评价失败，请先登录");
        }
        AppUser appUser = loginAppUser.getAppUser();
        if (Objects.isNull(appUser)) {
            throw new ServiceException("评价失败，用户不存在");
        }
        appReview.setUserId(appUser.getUserId());
        appReview.setNickName(appUser.getNickName());
        appReview.setEmail(appUser.getEmail());
        appReview.setMobile(appUser.getMobile());
        appReview.setUserName(appUser.getUserName());
        appReview.setAvatar(appUser.getAvatar());

        AppArtifact artifact = appArtifactMapper.selectAppArtifactByArtifactId(appReview.getArtifactId());
        if (Objects.isNull(artifact)) {
            throw new ServiceException("评价失败，文物不存在");
        }
        appReview.setArtifactName(artifact.getArtifactName());
        appReview.setPhotoUrl(artifact.getPhotoUrl());

        appReview.setCreateTime(DateUtils.getNowDate());
        return appReviewMapper.insertAppReview(appReview);
    }

    /**
     * 修改评价
     * 
     * @param appReview 评价
     * @return 结果
     */
    @Override
    public int updateAppReview(AppReview appReview)
    {
        return appReviewMapper.updateAppReview(appReview);
    }

    /**
     * 批量删除评价
     * 
     * @param reviewIds 需要删除的评价主键
     * @return 结果
     */
    @Override
    public int deleteAppReviewByReviewIds(Long[] reviewIds)
    {
        return appReviewMapper.deleteAppReviewByReviewIds(reviewIds);
    }

    /**
     * 删除评价信息
     * 
     * @param reviewId 评价主键
     * @return 结果
     */
    @Override
    public int deleteAppReviewByReviewId(Long reviewId)
    {
        return appReviewMapper.deleteAppReviewByReviewId(reviewId);
    }
}
